//
//  UploadProgressView.m
//  ZhaoHuoBanProject
//
//  Created by Dubaoquan on 2019/1/11.
//  Copyright © 2019 山东找伙伴. All rights reserved.
//

#import "UploadProgressView.h"
#import "CMLSubmitButton.h"
#import "AppConst.h"
@import UIKit;

@interface UploadProgressView()
@property (nonatomic,strong) UIView *contentView;
@property (nonatomic,strong) CMLSubmitButton *titleLabel;
@property (nonatomic,strong) UIProgressView *progressView;
@property (nonatomic,strong) UIWindow *overlayWindow;
@end

@implementation UploadProgressView

+(UploadProgressView *)sharedInstance
{
    __strong static UploadProgressView *sharedManager;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[UploadProgressView alloc] init];
    });
    
    return sharedManager;
}
-(instancetype)init{
    self  = [super init];
    if (self) {
        self.view.frame = self.contentView.bounds;
        [self.view addSubview:self.contentView];
    }
    return self;
}

-(void)showWithTitle:(NSString*)title progress:(float)progress{
    
    WEAK_SELF(weakSelf)
    dispatch_async(dispatch_get_main_queue(), ^{
        STRONG_SELF(strongSelf)
        if (strongSelf.view.superview == nil) {
            [strongSelf.overlayWindow addSubview:strongSelf.view];
        }
    
        strongSelf.titleLabel.doingStatusText = title;
        strongSelf.titleLabel.textColor = [UIColor blackColor];
        strongSelf.progressView.hidden = NO;
        if (strongSelf.titleLabel.payStatus!=SubmitStatusDoing) {
            [strongSelf.titleLabel setPayStatus:SubmitStatusDoing];
        }
        
        strongSelf.progressView.progress = progress;
        
    });
    
}
-(void)hideWithTitle:(NSString*)title{
    
    WEAK_SELF(weakSelf)
    dispatch_async(dispatch_get_main_queue(), ^{
        
        STRONG_SELF(strongSelf)
        strongSelf.progressView.hidden = YES;
        strongSelf.titleLabel.failedStatusText = title;
        strongSelf.titleLabel.textColor = [UIColor orangeColor];
        [strongSelf.titleLabel setPayStatus:SubmitStatusFailed];
        
        
        CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        pulse.duration = 1;
        pulse.repeatCount = 1;
        pulse.autoreverses = YES;
        pulse.fromValue = [NSNumber numberWithFloat:-0.02];
        pulse.toValue = [NSNumber numberWithFloat:0.005];
        [strongSelf.titleLabel.layer addAnimation:pulse forKey:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           strongSelf.overlayWindow = nil;
            [[UIApplication sharedApplication].delegate.window makeKeyAndVisible];
        });

    });
}

-(UIView*)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, StatusBarHeight+5)];
        _contentView.backgroundColor = MainBackGroudColor;
        [_contentView addSubview:self.titleLabel];
        [_contentView addSubview:self.progressView];
    }
    return _contentView;
}
-(CMLSubmitButton*)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[CMLSubmitButton alloc]initWithFrame:CGRectMake(5, 0, 90, StatusBarHeight) animationRadius:6];
        _titleLabel.textFont = [UIFont systemFontOfSize:12.0];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.normalStatusText = @"";
        _titleLabel.doingStatusText = @"发布中";
        _titleLabel.successStatusText = @"发布成功";
        _titleLabel.failedStatusText = @"上传失败";
    }
    return _titleLabel;
}
-(UIProgressView*)progressView{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(10, StatusBarHeight, SCREENWIDTH-20, 5)];
        _progressView.progressTintColor = [UIColor orangeColor];
        _progressView.trackTintColor = MainColor;
    }
    return _progressView;
}
- (UIWindow *)overlayWindow {
    if(!_overlayWindow){

        _overlayWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, StatusBarHeight+5)];
        _overlayWindow.backgroundColor = MainBackGroudColor;
        _overlayWindow.userInteractionEnabled = YES;
        _overlayWindow.windowLevel = UIWindowLevelStatusBar;
        _overlayWindow.userInteractionEnabled = YES;
        _overlayWindow.rootViewController = self;
        [_overlayWindow makeKeyAndVisible];

    }
    return _overlayWindow;
}

@end
